<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['name']"
                :trigger-change="true"
                org-fields="id,project_name,num,customer_name,project_manager,customer_id"
                dest-fields="saleContractId,name,code,custShortName,charger,customerId"
                code="contract_sale_select"
                @callback="popupCallback"
              />
              <a-input v-decorator="['saleContractId']" v-show="false"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['code']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['numCode']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='客户简称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['custShortName']" disabled></a-input>
              <a-input v-decorator="['customerId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['shortName', validatorRules.shortName]" placeholder="请输入项目简称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目经理" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false"
                                    v-decorator="['charger', validatorRules.charger]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="签订日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="签订日期" v-decorator="['signDate',validatorRules.signDate]"
                      :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='项目分类' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['type',validatorRules.type]" :trigger-change='true'
                                 dictCode='icm_project_type' placeholder='请选择项目分类'/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='项目状态' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['projectState',validatorRules.projectState]"
                                 :trigger-change='true'
                                 dictCode='hf_project_states' placeholder='请选择项目状态'/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目周期/周" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectCycle', validatorRules.projectCycle]" placeholder="请输入项目周期"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="预估人天" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['estimateWorkday', validatorRules.estimateWorkday]" placeholder="请输入预估人天,类型为数字"
                       type="number"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="完工比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualPersonDay', validatorRules.actualPersonDay]"
                       placeholder="请输入完工比例"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择计划开始时间" v-decorator="['planStartTime',validatorRules.planStartTime]"
                      :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择计划结束时间" v-decorator="['planEndTime',validatorRules.planEndTime]"
                      :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="实际回款" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualMoney']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['money']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="回款比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['ratioMoney']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="总计报销" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['totalReimbursement']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="延期天数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['delayDays']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目得分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectScore']"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" type='primary'>提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'PmProjectFormDetail',
    components: {},
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: () => {
        },
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          custShortName: {
            rules: [
              { required: true, message: '请选择客户!' }
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入项目名称!' }
            ]
          },
          code: {
            rules: [
              { required: true, message: '请输入项目编号!' },
              { validator: (rule, value, callback) => validateDuplicateValue('hf_pm_project', 'code', value, this.model.id, callback) }
            ]
          },
          shortName: {
            rules: [
              { required: true, message: '请输入项目简称!' },
              { validator: (rule, value, callback) => validateDuplicateValue('hf_pm_project', 'short_name', value, this.model.id, callback) }
            ]
          },
          charger: {
            rules: [
              { required: true, message: '请选择项目经理!' }
            ]
          },
          type: {
            rules: [
              { required: true, message: '请选择项目分类!' }
            ]
          },
          projectState: {
            rules: [
              { required: true, message: '请选择项目状态!' }
            ]
          },
          actualPersonDay: {
            rules: [
              { required: true, message: '请输入实际人天!' }
            ]
          },
          projectCycle: {
            rules: [
              { required: true, message: '请输入项目周期!' },
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
            ]
          },
          estimateWorkday: {
            rules: [
              { required: true, message: '请输入预估人天!' },
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
            ]
          },
          planStartTime: {
            rules: [
              { required: true, message: '请选择计划开始时间!' }
            ]
          },
          signDate: {
            rules: [
              { required: true, message: '请选择计划签订时间!' }
            ]
          },
          planEndTime: {
            rules: [
              { required: true, message: '请选择计划结束时间!' }
            ]
          }
        },
        url: {
          list: '/pm/pmProject/list',
          add: '/pm/pmProject/add',
          edit: '/pm/pmProject/edit',
          queryById: '/pm/pmProject/queryById'
        },
        fields: [
          'name', 'code', 'delayDays', 'projectScore', 'signDate', 'totalReimbursement', 'actualMoney', 'money', 'ratioMoney', 'signDate', 'shortName', 'numCode', 'custShortName', 'customerId', 'type', 'charger', 'actualPersonDay', 'saleContractId', 'projectState', 'projectCycle', 'estimateWorkday', 'planStartTime', 'planEndTime'
        ]
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created() {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, this.fields))
        })
      },
      //渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, this.fields))
      }
    }
  }
</script>